package tryThings;
import javax.media.opengl.GL;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.glu.GLU;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.media.opengl.GLCanvas;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import com.sun.opengl.util.Animator;
 
public class JoglDemo implements GLEventListener, KeyListener {

    float rotateT = 0.0f;
    float translateT = 0.0f;
    static GLU glu = new GLU();
    static GLCanvas canvas = new GLCanvas();
    static Frame frame = new Frame("Jogl 3D Shape/Rotation");
    static Animator animator = new Animator(canvas); 
    
    public void display(GLAutoDrawable gLDrawable) {
		final GL gl = gLDrawable.getGL();
		gl.glClear(GL.GL_COLOR_BUFFER_BIT);
		gl.glClear(GL.GL_DEPTH_BUFFER_BIT);
		gl.glLoadIdentity();
		gl.glTranslatef(0.0f, translateT, -0.5f);
	 
		//gl.glRotatef(rotateT, 1.0f, 0.0f, 0.0f);
		//gl.glRotatef(rotateT, 0.0f, 1.0f, 0.0f);
		//gl.glRotatef(rotateT, 0.0f, 0.0f, 1.0f);
		//gl.glRotatef(rotateT, 0.0f, 1.0f, 0.0f);
	 
		gl.glBegin(GL.GL_TRIANGLES);
	 
		// Front
		gl.glColor3f(0.0f, 1.0f, 1.0f); 
		gl.glVertex3f(0.0f, 1.0f, 0.0f);
		gl.glColor3f(0.0f, 0.0f, 1.0f); 
		gl.glVertex3f(-1.0f, -1.0f, 1.0f);
		gl.glColor3f(0.0f, 0.0f, 0.0f); 
		gl.glVertex3f(1.0f, -1.0f, 1.0f);
	 
		// Right Side Facing Front
		gl.glColor3f(0.0f, 1.0f, 1.0f); 
		gl.glVertex3f(0.0f, 1.0f, 0.0f);
		gl.glColor3f(0.0f, 0.0f, 1.0f); 
		gl.glVertex3f(1.0f, -1.0f, 1.0f);
		gl.glColor3f(0.0f, 0.0f, 0.0f); 
		gl.glVertex3f(0.0f, -1.0f, -1.0f);
	 
		// Left Side Facing Front
		gl.glColor3f(0.0f, 1.0f, 1.0f); 
		gl.glVertex3f(0.0f, 1.0f, 0.0f);
		gl.glColor3f(0.0f, 0.0f, 1.0f); 
		gl.glVertex3f(0.0f, -1.0f, -1.0f);
		gl.glColor3f(0.0f, 0.0f, 0.0f); 
		gl.glVertex3f(-1.0f, -1.0f, 1.0f);
	 
		// Bottom
		gl.glColor3f(0.0f, 0.0f, 0.0f); 
		gl.glVertex3f(-1.0f, -1.0f, 1.0f);
		gl.glColor3f(0.1f, 0.1f, 0.1f); 
		gl.glVertex3f(1.0f, -1.0f, 1.0f);
		gl.glColor3f(0.2f, 0.2f, 0.2f); 
		gl.glVertex3f(0.0f, -1.0f, -1.0f);
	 
		gl.glEnd();
	 
		//rotateT += 0.2f;
	
		translateT -= 0.005;
		
		
    }
 
    public void displayChanged(GLAutoDrawable gLDrawable, 
			       boolean modeChanged, boolean deviceChanged) {
    }
 
    public void init(GLAutoDrawable gLDrawable) {
		GL gl = gLDrawable.getGL();
		gl.glShadeModel(GL.GL_SMOOTH);
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		gl.glClearDepth(1.0f);
		gl.glEnable(GL.GL_DEPTH_TEST);
		gl.glDepthFunc(GL.GL_LEQUAL);
		gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
		gLDrawable.addKeyListener(this);
    }
 
    public void reshape(GLAutoDrawable gLDrawable, int x, 
			int y, int width, int height) {
		GL gl = gLDrawable.getGL();
		if(height <= 0) {
		    height = 1;
		}
		float h = (float)width / (float)height;
		gl.glMatrixMode(GL.GL_PROJECTION);
		gl.glLoadIdentity();
		glu.gluPerspective(50.0f, h, 1.0, 1000.0);
		gl.glMatrixMode(GL.GL_MODELVIEW);
		gl.glLoadIdentity();
    }
 
    public void keyPressed(KeyEvent e) {
		if(e.getKeyCode() == KeyEvent.VK_ESCAPE) {
		    exit();
		}
    }
 
    public void keyReleased(KeyEvent e) {
    }
 
    public void keyTyped(KeyEvent e) {
    }
 
    public static void exit(){
		animator.stop();
		frame.dispose();
		System.exit(0);
    }
 
    public static void main(String[] args) {
		canvas.addGLEventListener(new JoglDemo());
		frame.add(canvas);
		frame.setSize(640, 480);
//		frame.setUndecorated(true);
//		frame.setExtendedState(Frame.MAXIMIZED_BOTH);
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
			    exit();
			}
		    });
		frame.setVisible(true);
		animator.start();
		canvas.requestFocus();
    }
}
